package com.wxl.www.wxl.shiro.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxl.www.wxl.shiro.entity.SysPermission;
import com.wxl.www.wxl.shiro.entity.UserInfo;
import com.wxl.www.wxl.shiro.mapper.SysPermissionMapper;
import com.wxl.www.wxl.shiro.service.SysPermissionService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Resource
    private SysPermissionMapper sysPermissionMapper;


    @Override
    public List<SysPermission> getMenuAll(UserInfo userInfo) {
        List<SysPermission>  list=sysPermissionMapper.getMenuAll(userInfo.getId());
        for(SysPermission sysPermission:list){
            sysPermission.setSysPermissionList(sysPermissionMapper.findByParentId(sysPermission.getId()));
        }
        return list;
    }

    @Override
    public List<Map<String ,Object>> getTreeMenuAll() {
        List<Map<String ,Object>> map=sysPermissionMapper.getTreeMenuAll();
        for(Map<String ,Object> mapone:map){
            List<Map<String, Object>> list = sysPermissionMapper.findMenuByParentId(mapone.get("id").toString());
            for(Map<String ,Object> mapch:list){
                List<Map<String, Object>> menuCh = getMenuCh(mapch.get("id").toString());
                if(menuCh!=null){
                    mapch.put("children",menuCh);
                }
            }
            mapone.put("children",list);
        }
        return map;
    }

    @Override
    public List<SysPermission> findById(String id) {
        return sysPermissionMapper.findById(id);
    }


    public List<Map<String, Object>> getMenuCh(String id){
        return sysPermissionMapper.findMenuByParentId(id);
    }
}
